<template>
  <div class="content">
    <div class="herdertop">
      <img src="../../assets/top.png" alt="" class="topImg" />
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>首页配置（小程序）</el-breadcrumb-item>
        <el-breadcrumb-item>首页配置（小程序）</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="center" style="padding-top: 0px;">
      <div class="hander">首页配置（小程序）</div>
      <div class="orderList">
        <div class="left">
          <homeLeft
            :list="list"
            :list1="list1"
            :list2="list2"
            :list3="list3"
            :list4="list4"
            :list5="list5"
            :materialName="materialName"
            :positionName="positionName"
          />
        </div>
        <div class="right">
          <div class="righttop">
            <span>首页配置（小程序）</span>
            <el-button
              style="background-color:#fd804c;color: #fff;"
              size="small"
              @click="isRelease"
              >发布</el-button
            >
          </div>
          <el-table :data="tableData" border>
            <el-table-column prop="name" label="栏位名称">
              <template slot-scope="scope">
                <div>
                  {{ scope.row.name }}
                </div>
              </template>
            </el-table-column>
            <el-table-column prop="sort" label="展示位置">
              <template slot-scope="scope">
                <el-input-number
                  v-model="scope.row.sort"
                  controls-position="right"
                  @change="handleChange(scope.row)"
                  :min="0"
                  :max="100"
                ></el-input-number>
              </template>
            </el-table-column>
            <el-table-column prop="description" label="栏位类型">
              <template slot-scope="scope">
                <div>
                  {{ scope.row.description }}
                </div>
              </template>
            </el-table-column>
            <el-table-column label="操作">
              <template slot-scope="scope">
                <el-button
                  @click="isInfo(scope.row)"
                  style="color: #fc9e76;"
                  type="text"
                  size="small"
                  >详情</el-button
                >
                <el-button
                  style="color: #fc9e76;"
                  @click="isUpdate(scope.row)"
                  type="text"
                  size="small"
                  >修改</el-button
                >
                <el-button
                  type="text"
                  style="color: #fc9e76;"
                  size="small"
                  @click="isDel(scope.row)"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>

          <el-dialog title="商品详情" :visible.sync="formVisible">
            <!--  账号状态搜索信息 -->
            <div class="demo-input-suffix">
              <el-button
                style="background-color:#fd804c;color: #fff;"
                @click="isCopy"
                >复制卡片</el-button
              >
            </div>

            <el-form :model="formData">
              <el-form-item label="数据分组名称" :label-width="LabelWidth">
                <el-select
                  v-model="formData.hotGoods"
                  placeholder="请选择分组名称"
                >
                  <el-option
                    v-for="(item, index) in hotGoodsList"
                    :key="index"
                    :label="item.goodsClassifyName"
                    :value="item.goodsClassifyId"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
              <el-button @click="formVisible = false">取 消</el-button>
              <el-button
                style="background-color:#fd804c;color: #fff;"
                @click="isSubmitGoods"
                >确 定</el-button
              >
            </div>
          </el-dialog>

          <div class="block">
            <div class="left1"></div>
            <div>
              <el-pagination
                class="page"
                layout="total, sizes, prev, pager, next, jumper"
                @size-change="sizeChangeHandle"
                @current-change="currentChangeHandle"
                :current-page="page"
                :page-size="size"
                :total="total"
              >
              </el-pagination>
            </div>
          </div>
          <el-dialog title="修改" :visible.sync="dialogFormVisible">
            <el-form :model="form">
              <el-form-item label="栏位名称" :label-width="formLabelWidth">
                <el-input
                  v-model="form.name"
                  autocomplete="off"
                  maxlength="10"
                  show-word-limit
                ></el-input>
              </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
              <el-button @click="dialogFormVisible = false">取 消</el-button>
              <el-button
                style="background-color:#fd804c;color: #fff;"
                @click="isSubmit"
                >确 定</el-button
              >
            </div>
          </el-dialog>

          <el-dialog title="修改活动" :visible.sync="activityVisible">
            <!--修改-->
            <el-form :model="activityForm" ref="form">
              <el-form-item
                label="栏位名称"
                required
                prop="positionName"
                :label-width="formLabelWidth"
              >
                <el-input v-model="activityForm.positionName"></el-input>
              </el-form-item>
              <el-form-item
                label="栏位简介"
                required
                prop="positionSubName"
                :label-width="formLabelWidth"
              >
                <el-input v-model="activityForm.positionSubName"></el-input>
              </el-form-item>
              <el-form-item
                label="跳转名称"
                required
                prop="targetUrlName"
                :label-width="formLabelWidth"
              >
                <el-input v-model="activityForm.targetUrlName"></el-input>
              </el-form-item>
              <el-form-item
                label="跳转方式"
                required
                prop="targetType"
                :label-width="formLabelWidth"
              >
                <el-select
                  v-model="activityForm.targetType"
                  placeholder="请选择"
                >
                  <el-option label="应用内跳转" value="app"></el-option>
                  <el-option
                    label="跳转到第三方应用"
                    value="third_party"
                  ></el-option>
                  <el-option label="H5落地页" value="h5"></el-option>
                </el-select>
              </el-form-item>
              <el-form-item
                label="授权类型"
                required
                prop="authType"
                :label-width="formLabelWidth"
              >
                <el-select
                  v-model="activityForm.authType"
                  placeholder="请选择授权类型"
                >
                  <el-option label="无条件" value="needless"></el-option>
                  <el-option
                    label="授权星福生活登录"
                    value="platform_auth"
                  ></el-option>
                  <el-option
                    label="授权微盟登录"
                    value="weimob_auth"
                  ></el-option>
                </el-select>
              </el-form-item>
              <el-form-item
                label="链接"
                required
                prop="targetUrl"
                :label-width="formLabelWidth"
              >
                <el-input
                  v-model="activityForm.targetUrl"
                  style="width:70%;"
                ></el-input>
                <el-button type="text" @click="urlback"
                  >url配置规则说明</el-button
                >
              </el-form-item>
            </el-form>

            <div slot="footer" class="dialog-footer">
              <el-button @click="activityVisible = false">取 消</el-button>
              <el-button
                style="background-color:#fd804c;color: #fff;"
                @click="isActivitySubmit"
                >确 定</el-button
              >
            </div>
          </el-dialog>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import homeLeft from "./homeLeft.vue";
export default {
  name: "homeRight",
  components: { homeLeft },
  data() {
    return {
      page: 1,
      size: 20,
      total: 0,
      minNum: null,
      text: "",
      input: "",
      formLabelWidth: "80px",
      LabelWidth: "100px",
      activityForm: {
        authType: "",
        positionId: null,
        positionName: "",
        positionSubName: "",
        authType: "",
        targetType: "",
        targetUrl: "",
        targetUrlName: ""
      },
      form: {
        activityVo: null,
        description: "",
        hotGoods: null,
        name: "",
        pageId: null,
        positionCode: "",
        positionId: null,
        positionStatus: "",
        positionType: "",
        sort: ""
      },
      hotGoodsList: [],
      formData: {
        hotGoods: ""
      },
      positionType: "",
      name: "",
      description: "",
      dialogFormVisible: false,
      activityVisible: false,
      formVisible: false,
      positionId: "",
      hotGoodsNum: "0",
      tableData: [],
      list: [], //banner图
      list1: [], //金刚区
      list2: [], //通知
      list3: [], //福利
      list4: [], //活动
      materialName: "",
      list5: [], //热门商品
      positionName: ""
    };
  },
  activated() {
    this.DetailByPage();
    this.getlist();
    this.hotGoodslist();
  },
  methods: {
    //url链接
    urlback() {
      window.location.href = `https://golte-platform.oss-cn-shanghai.aliyuncs.com/excelFile/20211124%20TargetUrl%E9%85%8D%E7%BD%AE%E8%A7%84%E5%88%99.xlsx`;
    },
    isInfo(data) {
      this.positionId = data.positionId;
      if (data.positionCode == "banner") {
        this.$router.push({
          path: "/homeApplet",
          query: {
            positionType: data.positionType,
            positionId: data.positionId,
            name: data.name,
            description: data.description
          }
        });
      } else if (data.positionCode == "kingKong") {
        this.$router.push({
          path: "/kumgangApplet",
          query: {
            positionType: data.positionType,
            positionId: data.positionId,
            name: data.name,
            description: data.description
          }
        });
      } else if (data.positionCode == "notice") {
        this.$router.push({
          path: "/messageApplet",
          query: {
            positionType: data.positionType,
            positionId: data.positionId,
            name: data.name,
            description: data.description
          }
        });
      } else if (data.positionCode == "activity") {
        this.$router.push({
          path: "/activityApplet",
          query: {
            positionType: data.positionType,
            positionId: data.positionId,
            name: data.name,
            description: data.description
          }
        });
      } else if (data.positionCode == "hotGoods") {
        this.formVisible = true;
        this.hotGoodsNum = data.hotGoods;
        (this.positionType = data.positionType),
          (this.positionId = data.positionId),
          (this.name = data.name),
          (this.description = data.description);

        this.fetchHotGoodsMessageList(data.positionId);
      } else if (data.positionCode == "welfare") {
        this.$router.push({
          path: "/welfareApplet",
          query: {
            positionType: data.positionType,
            positionId: data.positionId,
            name: data.name,
            description: data.description
          }
        });
      }
    },
    isActivitySubmit() {
      this.$http({
        url: `/cmsadmin/appIndex/backManager/updatePositionMessage`,
        method: "POST",
        data: {
          positionId: this.activityForm.positionId,
          authType: this.activityForm.authType,
          positionName: this.activityForm.positionName,
          positionSubName: this.activityForm.positionSubName,
          positionType: this.activityForm.authType,
          targetType: this.activityForm.targetType,
          targetUrl: this.activityForm.targetUrl,
          targetUrlName: this.activityForm.targetUrlName
        }
      })
        .then(res => {
          if (res.success == 1) {
            this.$message.success("修改成功");
            this.DetailByPage();
            this.activityVisible = false;
          } else {
            this.$message.error(res.errorMsg);
          }
        })
        .catch(() => {
          this.$message.error("修改失败");
        });
    },
    isSubmit() {
      this.$http({
        url: `/cmsadmin/appIndex/backManager/updatePositionName`,
        method: "POST",
        data: {
          positionId: this.form.positionId,
          positionName: this.form.name || ""
        }
      })
        .then(res => {
          if (res.success == 1) {
            this.$message.success("修改成功");
            this.DetailByPage();
          }
          (this.form = {
            activityVo: null,
            description: "",
            hotGoods: null,
            name: "",
            pageId: null,
            positionCode: "",
            positionId: null,
            positionStatus: "",
            positionType: "",
            sort: ""
          }),
            (this.dialogFormVisible = false);
        })
        .catch(() => {
          this.$message.error("修改失败");
        });
    },
    isSubmitGoods() {
      this.$http({
        url: `/cmsadmin/appIndex/backManager/configHotGoods`,
        method: "POST",
        headers: {
          "platform-type": 1
        },
        data: {
          addOrUpdate: this.hotGoodsNum == "0" ? "" : "update",
          goodsClassifyId: this.formData.hotGoods,
          positionId: this.positionId
        }
      })
        .then(res => {
          if (res.success == 1) {
            this.$message.success("修改成功");
            this.DetailByPage();
          }

          this.formVisible = false;
        })
        .catch(() => {
          this.$message.error("修改失败");
        });
    },
    isUpdate(data) {
      //console.log("data,,,,,", data);
      if (data.positionCode != "activity") {
        this.dialogFormVisible = true;
      } else {
        this.activityVisible = true;
        this.activityForm = data.activityVo;
        this.positionId = data.positionId;
      }

      this.form = data;
    },
    isDel(data) {
      //console.log("data,,,,,", data);
      this.$http({
        url: `/cmsadmin/appIndex/backManager/deletePositionById`,
        method: "POST",
        data: {
          positionCode: data.positionCode,
          positionId: data.positionId
        }
      })
        .then(res => {
          if (res.success == 1) {
            this.$message.success("删除成功");
            this.DetailByPage();
          }
        })
        .catch(() => {
          this.$message.error("删除失败");
        });
    },
    //分页获取列表接口调用
    DetailByPage() {
      this.$http({
        url: `/cmsadmin/appIndex/backManager/fetchIndexBackList`,
        method: "GET",
        headers: {
          "platform-type": 1
        },
        params: {
          page: this.page,
          size: this.size
        }
      }).then(res => {
        this.tableData = res.list;
        this.total = parseInt(res.total);
      });
    },
    //分页获取商品列表接口调用
    fetchHotGoodsMessageList(positionId) {
      this.$http({
        url: `/cmsadmin/appIndex/backManager/fetchHotGoodsMessageList`,
        method: "GET",
        headers: {
          "platform-type": 1
        },
        params: {
          positionId,
          page: this.page,
          size: this.size
        }
      }).then(res => {
        this.hotGoodsList = res.list;
      });
    },
    // 每页数
    sizeChangeHandle(val) {
      this.size = val;
      this.page = 1;
      this.DetailByPage();
    },
    // 当前页
    currentChangeHandle(val) {
      this.page = val;
      this.DetailByPage();
    },
    //修改栏位状态
    isSwitch(data) {
      //console.log("data.positionStatus,,,,", data.positionStatus);
      this.$http({
        url: `/cmsadmin/homeService/config/index/updateMaterialStatus`,
        method: "POST",
        data: {
          positionId: data.positionId,
          positionStatus: data.positionStatus == true ? 1 : 0
        }
      })
        .then(res => {
          if (res.success == 1) {
            this.$message.success("修改成功");
            this.DetailByPage();
          } else {
            this.$message.error(res.errorMsg);
          }
        })
        .catch(() => {
          this.$message.error("修改失败");
        });
    },
    isRelease() {
      this.$http({
        url: `/cmsadmin/appIndex/backManager/updatePositionStatus`,
        method: "POST",
        headers: {
          "platform-type": 1
        },
        data: {}
      })
        .then(res => {
          if (res.success == 1) {
            this.$message.success("发布成功");
            this.DetailByPage();
          } else {
            this.$message.error(res.errorMsg);
          }
        })
        .catch(() => {
          this.$message.error("发布失败");
        });
    },
    isCopy() {
      this.$http({
        url: "/cmsadmin/appIndex/backManager/copyPosition",
        method: "POST",
        data: {
          positionId: this.positionId,
          positionName: this.name,
          positionType: this.positionType,
          subName: this.description,
          target: ""
        }
      })
        .then(res => {
          if (res.success === 0) {
            this.$message.error(res.errorMsg);
          } else {
            this.$message.success("复制成功");
            this.$router.push({
              path: "/ManagementHome"
            });
          }
        })
        .catch(() => {
          this.$message.error("复制失败");
        });
    },
    // 控制下移按钮的显示于隐藏
    getFormLength(index) {
      if (index === this.tableData.length - 1) return true;
      else return false;
    },
    //左边列表
    getlist() {
      this.$http({
        url: "/cmsadmin/appIndex/backManager/fetchAllPositionMaterialList",
        method: "POST",
        headers: {
          "platform-type": 1
        }
      }).then(res => {
        //console.log(res, '左边列表')
        let banner = res.filter(e => e.description === "轮播banner（多个）");
        this.list = banner[0].materialList[0].materialContent;
        let kingKong = res.filter(e => e.description === "金刚区");
        this.list1 = kingKong[0].materialList.splice(0, 4);
        let notice = res.filter(e => e.description === "通知");
        this.list2 = notice[0].materialList;
        let welfare = res.filter(e => e.description === "福利");
        this.list3 = welfare[0].materialList;
        let activity = res.filter(e => e.description === "活动");
        this.list4 = activity[0].materialList[0].materialContent;
        this.materialName = activity[0].materialList[0].materialName;
      });
    },
    // 热门商品
    hotGoodslist() {
      this.$http({
        url: "/cmsadmin/appIndex/backManager/fetchHotGoodsGroupPositionId",
        method: "get",
        headers: {
          "platform-type": 1
        }
      }).then(res => {
        //console.log(res[0].goodsList, '热门商品')
        if (res.length > 0) {
          this.positionName = res[0].positionName;
          this.list5 = res[0].goodsList;
        }
      });
    },
    //展示位置
    handleChange(item) {
      //console.log(item);
      this.$http({
        url: "/cmsadmin/appIndex/backManager/updatePositionSort",
        method: "POST",
        data: {
          positionId: item.positionId,
          sort: item.sort
        }
      })
        .then(res => {
          if (res.success == 0) {
            this.$message.error(res.errorMsg);
          } else {
            this.$message.success("位置移动成功");
          }
          //console.log(res, '展示位置')
        })
        .catch(() => {
          this.$message.error("位置移动失败");
        });
    }
  }
};
</script>

<style lang="scss" scoped>
.block {
  width: 100%;
  height: 70px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  box-sizing: border-box;
  padding: 0 10px 0 10px;
}
.left1 {
  font-size: 15px;
}
.demo-input-suffix {
  position: absolute;
  right: 30px;
  top: 40px;
  margin-left: 2%;
  margin-bottom: 2%;
}
.entirety {
  width: 95%;
  margin: 10px;
  border: 7px solid #fafafa;
  border-radius: 10px;
  .basic {
    font-size: 18px;
    font-weight: 900;
    margin-top: 2%;
    margin-bottom: 1%;

    margin-left: 1%;
  }
}
.hander {
  width: 100%;
  height: 50px;
  line-height: 50px;
  font-size: 20px;
  font-weight: 900;
}
.herdertop {
  height: 8vh;
  display: flex;
  align-items: center;
  padding: 0 20px;
  box-sizing: border-box;
}
.topImg {
  margin-right: 20px;
}
.orderList {
  background-color: #ebeef1;
  padding: 10px 10px 10px 10px;
  display: flex;
  border-radius: 20px;
  .left {
    flex: 1;
    height: 100%;
    background-color: #f3f3f3;
    margin-right: 10px;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
  }
  .right {
    flex: 2;
    background-color: #fff;
    padding: 10px;
  }
}
.righttop {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 18px;
  width: 100%;
  font-weight: 700;
  margin-bottom: 10px;
}
/deep/ .el-input-number {
  width: 100px;
}
</style>
